package com.rundow.center.manager.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rundow.center.manager.mapper.DriverAttributeMapper;
import com.rundow.center.manager.mapper.DriverPropertyMapper;
import com.rundow.center.manager.mapper.PointAttributeMapper;
import com.rundow.center.manager.service.DriverAttributeService;
import com.rundow.center.manager.service.DriverPropertyService;
import com.rundow.center.manager.service.PointAttributeService;
import com.rundow.center.manager.service.SchedulePropertyService;
import com.rundow.common.base.bean.driver.DriverProperty;
import com.rundow.common.base.bean.driver.ScheduleProperty;
import com.rundow.common.base.dto.DriverPropertyDto;
import com.rundow.common.base.model.DriverAttribute;
import com.rundow.common.base.model.PointAttribute;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class DriverPropertyServiceImpl implements DriverPropertyService {

    @Resource
    private DriverPropertyMapper mapper;
    @Resource
    private PointAttributeService pointAttributeService;
    @Resource
    private DriverAttributeService driverAttributeService;

    @Resource
    private SchedulePropertyService schedulePropertyService;


    @Override
    public List<DriverProperty> selectList(DriverProperty driverProperty) {
         LambdaQueryWrapper<DriverProperty> queryWrapper= fuzzyQuery(null);
         List<DriverProperty> list = mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public DriverProperty selectByDriverId(String driverId) {
        DriverPropertyDto dto= new DriverPropertyDto();
        dto.setDriverId(driverId);
        LambdaQueryWrapper<DriverProperty> queryWrapper= fuzzyQuery(dto);
        return mapper.selectOne(queryWrapper);
    }

    @Override
    public DriverProperty add(DriverProperty type) {
     mapper.insert(type);
     return type;
    }

    @Override
    public boolean delete(String id) {
        return mapper.deleteById(id)>0;
    }

    @Override
    public DriverProperty update(DriverProperty type) {
        return null;
    }

    @Override
    public DriverProperty selectById(String id){ return mapper.selectById(id);}


    @Override
    public Page<DriverProperty> list(DriverPropertyDto dto) {
      return   mapper.selectPage(new Page<>(dto.getPage().getCurrent(),dto.getPage().getSize()),fuzzyQuery(dto));
    }

    @Override
    public LambdaQueryWrapper<DriverProperty> fuzzyQuery(DriverPropertyDto dto) {
        LambdaQueryWrapper<DriverProperty> queryWrapper = Wrappers.<DriverProperty>query().lambda();
        if (ObjectUtil.isNotNull(dto)) {
            queryWrapper.eq(StrUtil.isNotEmpty(dto.getDriverId()), DriverProperty::getDriverId, dto.getDriverId());

        }
        return queryWrapper;
    }

    @Override
    public  DriverProperty selectAllDriverPropertyById(String driverId){
         DriverProperty driverProperty = selectByDriverId(driverId);
        final List<PointAttribute> pointAttributes = pointAttributeService.selectByDriverId(driverId);
        driverProperty.setPointAttribute(pointAttributes);
        final List<DriverAttribute> driverAttributes = driverAttributeService.selectByDriverId(driverId);
        driverProperty.setDriverAttribute(driverAttributes);
        final ScheduleProperty scheduleProperty = schedulePropertyService.selectOneByDriverId(driverId);
        driverProperty.setSchedule(scheduleProperty);
        return driverProperty;
    }
}
